
package com.agileai.pm.module.base.handler;

import com.agileai.common.KeyGenerator;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.StandardEditHandler;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.pm.module.base.service.TeamStaffManage;
import com.agileai.util.CryptionUtil;
import com.agileai.util.MapUtil;

public class TeamStaffManageEditHandler
        extends StandardEditHandler {
    public TeamStaffManageEditHandler() {
        super();
        this.listHandlerClass = TeamStaffManageListHandler.class;
        this.serviceId = buildServiceId(TeamStaffManage.class);
       
    }
    private static String defDuplicateMsg = "当前编码已存在，请检查！";
    public ViewRenderer prepareDisplay(DataParam param) {
		String operaType = param.get(OperaType.KEY);
		if (isReqRecordOperaType(operaType)){
			DataRow record = getService().getRecord(param);
			this.setAttributes(record);	
		}
		this.setAttributes(param);
		this.setOperaType(operaType);
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}
    
    protected void processPageAttributes(DataParam param) {
        setAttribute("USER_SEX",FormSelectFactory.create("USER_SEX").addSelectedValue(getOperaAttributeValue("USER_SEX","M")));
        setAttribute("PP_POST",FormSelectFactory.create("PP_POST").addSelectedValue(getOperaAttributeValue("PP_POST","MANAGER")));
        setAttribute("USER_TYPE",FormSelectFactory.create("USER_TYPE").addSelectedValue(getOperaAttributeValue("USER_TYPE","External")));
    }
    
	public ViewRenderer doSaveAction(DataParam param){
		String rootId = "00000000-0000-0000-00000000000000000";
		String state = "1";
		String sgId = KeyGenerator.instance().genKey();
		param.put("PP_ID",sgId);
		param.put("GRP_ID",rootId);
		param.put("USER_STATE",state);
		int num = getService().getMaxNum(param);
		param.put("USER_SORT",num);
		String userCode = param.getString("USER_CODE");
		String pwd=CryptionUtil.md5Hex(userCode);
		param.put("USER_PWD",pwd);
		String operateType = param.get(OperaType.KEY);
		if (OperaType.CREATE.equals(operateType)){
			getService().createRecord(param);
			getService().insertStaffInfo(param);
			getService().insertUserGrp(param);
			param.put("RG_ID", "67E9AE43-0967-4943-9740-104ED1EB4D2A");
			getService().insertUserRole(param);
			
		}
		else if (OperaType.UPDATE.equals(operateType)){
			getService().updateRecord(param);
			getService().updateStaffInfo(param);
		}
		return new RedirectRenderer(getHandlerURL(listHandlerClass));
	}
	@PageAction
	public ViewRenderer checkUnique(DataParam param){
		String responseText = "";
		String operateType = param.get(OperaType.KEY);
		DataRow record = getService().getRecord(param);
		if (OperaType.CREATE.equals(operateType)){
			String userCode = param.get("USER_CODE");
			if (!MapUtil.isNullOrEmpty(record) && userCode.equals(record.stringValue("USER_CODE"))){
				responseText = defDuplicateMsg;
			}		
		}
		return new AjaxRenderer(responseText);
	}
	
    protected TeamStaffManage getService() {
        return (TeamStaffManage) this.lookupService(this.getServiceId());
    }
}
